<template>
    <div>
        <h3>房型增加</h3>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td align="right">房型名称</td>
                    <td>
                        <input type="text" v-model="info.houseTypeName">
                    </td>
                </tr>
                <tr>
                    <td align="right">房型图片</td>
                    <td>
                        <input type="file" @change="imgs">
                        <img :src="info.photo" width="50" height="50" v-if="info.photo != ''">
                    </td>
                </tr>
                <tr>
                    <td align="right">房型面积</td>
                    <td>
                        <input type="text" v-model="info.area">
                    </td>
                </tr>
                <tr>
                    <td align="right">房型价格</td>
                    <td>
                        <input type="text" v-model="info.price">
                    </td>
                </tr>
                <tr>
                    <td align="right">最多入住人数</td>
                    <td>
                        <input type="text" v-model="info.personCount">
                    </td>
                </tr>
                <tr>
                    <td align="right">是否有窗</td>
                    <td>
                        <input type="radio" name="iswin" v-model="info.isWindow" :value="false">有
                        <input type="radio" name="iswin" v-model="info.isWindow" :value="true">否
                    </td>
                </tr>
                <tr>
                    <td align="right">状态</td>
                    <td>
                        <input type="radio" name="state" v-model="info.state" :value="1">发布
                        <input type="radio" name="state" v-model="info.state" :value="0">不发布
                    </td>
                </tr>
                <tr>
                    <td align="right"></td>
                    <td>
                        <input type="button" value="确定" class="btn btn-success" @click="add">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, registerRuntimeCompiler } from 'vue'
import axios from 'axios';
import { useRouter, useRoute } from 'vue-router';
const router = useRouter();
const route = useRoute();
let info = reactive({
    "houseTypeName": "",
    "photo": "",
    "area": '',
    "isWindow": '',
    "personCount": '',
    "price": '',
    "state": ''
})
const add = () => {
    if (info.houseTypeName == "") {
        alert('房型名称不能为空')
        return
    }
    if (info.photo == "") {
        alert('图片不能为空')
        return
    }
    if (info.area == "") {
        alert('面积不能为空')
        return
    }
    if (info.personCount == "") {
        alert('最多入住人数不能为空')
        return
    }
    if (info.price == "") {
        alert('价格不能为空')
        return
    }
    if (info.state == "") {
        alert('状态必选')
        return
    }
    axios({
        url: '/api/HouseType/AddHousetype',
        method: 'post',
        data: info
    })
        .then((res) => {
            console.log(res);
            if (res.data == -1) {
                alert('房型名称重复')
                return
            }
            if (res.data > 0) {
                alert('添加成功')
                return
            }
            else {
                alert('添加失败')
                return
            }
        })
        .catch((err) => {
            console.log(err);

        })
}
const imgs = (e: any) => {
    var obj = e.target.files[0]
    let f = new FormData()
    f.append("file", obj)
    axios({
        url: '/api/imgs/Img',
        method: 'post',
        data: f
    })
        .then((res) => {
            console.log(res);
            if (res.data == "不是图片") {
                alert('不是图片')
                return
            }
            if (res.data == "大于2M") {
                alert('大于2M')
                return
            }
            info.photo = res.data
        })
        .catch((err) => {
            console.log(err);

        })
}
</script>

<style scoped></style>